#include "testwindow.h"
#include <QTimer>
#include <QSharedMemory>
#include "hdcontroller.h"
#include "hdshareddata.h"
#include <qpalette.h>


QBuffer *buffer;
int down, moved, up;
TestWindow::TestWindow(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	//Prepare UI
	ui.setupUi(this);
	this->Manager = new ChimeraManager();
	ui.GLViewer->setMouseTracking(true);
	ui.GLViewer->setFocus();

	//init tools and stuff
	Tool1 = new gfxTool(0, 5, 5, Selecting, Vertex);
	Tool2 = new gfxTool(0, 5, 5, Selecting, Vertex);
	ui.GLViewer->Tools.append(Tool1);
	ui.GLViewer->Tools.append(Tool2);
	_mainMesh = 0;
	firstToolProp = secondToolProp = 0;

	//this lines will be removed
	controllers.append(new HDController(this, 0));
	connect(controllers[0], SIGNAL(controllerPressed(HDController*)), this, SLOT(controllerButtonPressed(HDController*)));
	connect(controllers[0], SIGNAL(controllerMoving(HDController*)), this, SLOT(controllerButtonMoving(HDController*)));
	connect(controllers[0], SIGNAL(controllerReleased(HDController*)), this, SLOT(controllerButtonReleased(HDController*)));
	controllers.append(new HDController(this, 1));
	down = 0;
	moved = 0;
	up = 0;
	//ends here

	//combos
	ui.cmbPMode1->addItem("Selection");
	ui.cmbPMode1->addItem("Vertex");
	ui.cmbPMode1->addItem("XFlattening");
	ui.cmbPMode1->addItem("YFlattening");
	ui.cmbPMode1->addItem("ZFlattening");
	ui.cmbPMode1->addItem("Tessellation");
	ui.cmbPMode1->addItem("Navigation");
	ui.cmbPMode1->setCurrentIndex(0);

	ui.cmbSMode1->addItem("Selection");
	ui.cmbSMode1->addItem("Vertex");
	ui.cmbSMode1->addItem("XFlattening");
	ui.cmbSMode1->addItem("YFlattening");
	ui.cmbSMode1->addItem("ZFlattening");
	ui.cmbSMode1->addItem("Tessellation");
	ui.cmbSMode1->addItem("Navigation");
	ui.cmbSMode1->setCurrentIndex(1);

	ui.cmbPMode2->addItem("Selection");
	ui.cmbPMode2->addItem("Vertex");
	ui.cmbPMode2->addItem("XFlattening");
	ui.cmbPMode2->addItem("YFlattening");
	ui.cmbPMode2->addItem("ZFlattening");
	ui.cmbPMode2->addItem("Tessellation");
	ui.cmbPMode2->addItem("Navigation");
	ui.cmbPMode2->setCurrentIndex(0);

	ui.cmbSMode2->addItem("Selection");
	ui.cmbSMode2->addItem("Vertex");
	ui.cmbSMode2->addItem("XFlattening");
	ui.cmbSMode2->addItem("YFlattening");
	ui.cmbSMode2->addItem("ZFlattening");
	ui.cmbSMode2->addItem("Tessellation");
	ui.cmbSMode2->addItem("Navigation");
	ui.cmbSMode2->setCurrentIndex(1);

	//prepare edit controls
	ActivePalette = ui.sbRadius1->palette();
	ActivePalette.setColor( QPalette::Text, Qt::red );

	InactivePalette = ui.sbRadius1->palette();
	InactivePalette.setColor( QPalette::Text, Qt::black );


	//Shared Memory stuff
	timer = new QTimer();
	timer->setInterval(1000/40);
	connect(timer, SIGNAL(timeout()), this, SLOT(readFromSharedMemory()));
	timer->start();

	sharedMemory = new QSharedMemory("WorldCoordinate");
	sharedMemory->attach();

	buffer = new QBuffer(new QByteArray(sharedMemory->size(), (char)0), 0);


	//navigation
	NavigationPoint = new Vertex3f(0, 0, 0);
}

TestWindow::~TestWindow()
{
	sharedMemory->detach();
	delete Tool1, Tool2;
	delete Manager;
}

//UI HANDLING
void TestWindow::on_btnReload_clicked()
{
	Tool1->FreeSelectedAreaInformation();
	Tool2->FreeSelectedAreaInformation();
	Manager->Load(ui.txtPath->text());
	_mainMesh = ui.GLViewer->_mainMesh = Manager->MainMesh;
	ui.GLViewer->updateGL();
}

//Tools Properties
void TestWindow::on_sbRadius1_valueChanged(int value)
{
	Tool1->Radius = value;
}
void TestWindow::on_sbRadius2_valueChanged(int value)
{
	Tool2->Radius = value;
}
void TestWindow::on_sbNLength1_valueChanged(int value)
{
	Tool1->NeighborsLength = value;
}
void TestWindow::on_sbNLength2_valueChanged(int value)
{
	Tool2->NeighborsLength = value;
}
void TestWindow::on_sbGRate1_valueChanged(int value)
{
	Tool1->GrowthRate = value;
}
void TestWindow::on_sbGRate2_valueChanged(int value)
{
	Tool2->GrowthRate = value;
}
void TestWindow::on_cmbPMode1_currentIndexChanged(int index)
{
	switch(index)
	{
	case 0:
		Tool1->PrimaryMode = Selecting;
		break;
	case 1:
		Tool1->PrimaryMode = Vertex;
		break;
	case 2:
		Tool1->PrimaryMode = XFlattenning;
		break;
	case 3:
		Tool1->PrimaryMode = YFlattenning;
		break;
	case 4:
		Tool1->PrimaryMode = ZFlattenning;
		break;
	case 5:
		Tool1->PrimaryMode = Tessellation;
		break;
	case 6:
		Tool1->PrimaryMode = Navigation;
		break;
	}
}

void TestWindow::on_cmbSMode1_currentIndexChanged(int index)
{
	switch(index)
	{
	case 0:
		Tool1->SecondaryMode = Selecting;
		break;
	case 1:
		Tool1->SecondaryMode = Vertex;
		break;
	case 2:
		Tool1->SecondaryMode = XFlattenning;
		break;
	case 3:
		Tool1->SecondaryMode = YFlattenning;
		break;
	case 4:
		Tool1->SecondaryMode = ZFlattenning;
		break;
	case 5:
		Tool1->SecondaryMode = Tessellation;
		break;
	case 6:
		Tool1->SecondaryMode = Navigation;
		break;
	}
}

void TestWindow::on_cmbPMode2_currentIndexChanged(int index)
{
	switch(index)
	{
	case 0:
		Tool2->PrimaryMode = Selecting;
		break;
	case 1:
		Tool2->PrimaryMode = Vertex;
		break;
	case 2:
		Tool2->PrimaryMode = XFlattenning;
		break;
	case 3:
		Tool2->PrimaryMode = YFlattenning;
		break;
	case 4:
		Tool2->PrimaryMode = ZFlattenning;
		break;
	case 5:
		Tool2->PrimaryMode = Tessellation;
		break;
	case 6:
		Tool2->PrimaryMode = Navigation;
		break;
	}
}

void TestWindow::on_cmbSMode2_currentIndexChanged(int index)
{
	switch(index)
	{
	case 0:
		Tool2->SecondaryMode = Selecting;
		break;
	case 1:
		Tool2->SecondaryMode = Vertex;
		break;
	case 2:
		Tool2->SecondaryMode = XFlattenning;
		break;
	case 3:
		Tool2->SecondaryMode = YFlattenning;
		break;
	case 4:
		Tool2->SecondaryMode = ZFlattenning;
		break;
	case 5:
		Tool2->SecondaryMode = Tessellation;
		break;
	case 6:
		Tool2->SecondaryMode = Navigation;
		break;
	}
}

//Controllers Handling
void TestWindow::controllerButtonPressed(HDController* sender)
{
	down++;
	ui.txtDown->setText(QString::number(down));
	if(_mainMesh != 0 && ui.GLViewer->Tools.count() > 0)
	{
		gfxTool *tool = ui.GLViewer->Tools.at(sender->GetControllerId());
		if(tool->PrimaryMode == EditingProperties && !sender->ringPressed())
		{
			if(sender->GetControllerId() == 0)
				EditFirstToolProperties(sender);
			else EditSecondToolProperties(sender);
			return;
		}
		if(sender->thumbPressed())
		{
			ui.GLViewer->first = tool->SelectedVertex;
			*ui.GLViewer->tmpVertex = *(ui.GLViewer->first);
			if(tool->Area != 0 && tool->PrimaryMode == Tessellation)
			{
				_mainMesh->TesselateAroundVertices(tool);
			}
			else if(tool->PrimaryMode == XFlattenning || tool->PrimaryMode == YFlattenning || tool->PrimaryMode == ZFlattenning)
			{
				if(tool->HasSelection)
				{
					_mainMesh->AffectSelectedArea1dGaussian(tool, ui.GLViewer->target);
				}
			}
			else if(tool->PrimaryMode == Navigation)
			{
				*NavigationPoint = *(tool->Position);
			}
		}

		if(sender->indexPressed())
		{
			if(tool->PrimaryMode != Navigation)
				tool->FreeSelectedAreaInformation();
		}

		if(sender->middlePressed())
		{
			tool->SwitchModes();
		}

		if(sender->ringPressed())
		{
			if(tool->PrimaryMode != EditingProperties)
			{
				tool->PrimaryMode = EditingProperties;
				if(sender->GetControllerId() == 0)
					EditFirstToolProperties(sender);
				else EditSecondToolProperties(sender);
			}
			else
			{
				if(sender->GetControllerId() == 0)
				{
					int index = ui.cmbPMode1->currentIndex();
					switch(index)
					{
					case 0:
						Tool1->PrimaryMode = Selecting;
						break;
					case 1:
						Tool1->PrimaryMode = Vertex;
						break;
					case 2:
						Tool1->PrimaryMode = XFlattenning;
						break;
					case 3:
						Tool1->PrimaryMode = YFlattenning;
						break;
					case 4:
						Tool1->PrimaryMode = ZFlattenning;
						break;
					case 5:
						Tool1->PrimaryMode = Tessellation;
						break;
					case 6:
						Tool1->PrimaryMode = Navigation;
						break;
					}
					EditFirstToolProperties(sender);
				}
				else
				{
					int index = ui.cmbPMode2->currentIndex();
					switch(index)
					{
					case 0:
						Tool2->PrimaryMode = Selecting;
						break;
					case 1:
						Tool2->PrimaryMode = Vertex;
						break;
					case 2:
						Tool2->PrimaryMode = XFlattenning;
						break;
					case 3:
						Tool2->PrimaryMode = YFlattenning;
						break;
					case 4:
						Tool2->PrimaryMode = ZFlattenning;
						break;
					case 5:
						Tool2->PrimaryMode = Tessellation;
						break;
					case 6:
						Tool2->PrimaryMode = Navigation;
						break;
					}
					EditSecondToolProperties(sender);
				}
			}
		}
	}
}

void TestWindow::controllerButtonMoving(HDController* sender)
{
	moved++;
	ui.txtMove->setText(QString::number(moved));
	if(_mainMesh && ui.GLViewer->Tools.count() > 0)
	{
		gfxTool *tool = ui.GLViewer->Tools.at(sender->GetControllerId());
		if(tool->PrimaryMode == Vertex)
		{
			if(sender->thumbPressed())
			{
				if(tool->Area)
					_mainMesh->AffectSelectedArea1dGaussian(tool, tool->Position);
			}
			else if(!tool->HasSelection) 
			{
				tool->SelectedVertex = _mainMesh->GetNearestVertex(tool->Position);
				_mainMesh->GetAffectedAreaInformation(tool);
			}
		}
		else if(tool->PrimaryMode == XFlattenning || tool->PrimaryMode == YFlattenning || tool->PrimaryMode == ZFlattenning)
		{
			if(sender->thumbPressed())
			{
				if(!tool->HasSelection)
				{
					tool->SelectedVertex = _mainMesh->GetNearestVertex(tool->Position);
					ui.GLViewer->second = tool->SelectedVertex;
					_mainMesh->GetAffectedAreaInformation(tool, ui.GLViewer->first, ui.GLViewer->second);
					_mainMesh->AffectSelectedArea1dGaussian(tool, ui.GLViewer->target);
					ui.GLViewer->first = ui.GLViewer->second;
				}
			}
		}
		else if(tool->PrimaryMode == Selecting)
		{
			tool->SelectedVertex = _mainMesh->GetNearestVertex(tool->Position);
			if(!tool->HasSelection)
				_mainMesh->GetAffectedAreaInformation(tool);
			if(sender->thumbPressed())
			{
				if(!tool->HasSelection)
					ui.GLViewer->target = tool->SelectedVertex;
				ui.GLViewer->second = tool->SelectedVertex;
				_mainMesh->GetAffectedAreaInformation(tool, ui.GLViewer->first, ui.GLViewer->second);
				ui.GLViewer->first = ui.GLViewer->second;
			}
		}
		else if(tool->PrimaryMode == Navigation)
		{
			if(sender->thumbPressed())
			{
				float dx = tool->Position->X - NavigationPoint->X;
				float dy = tool->Position->Y - NavigationPoint->Y;
				ui.GLViewer->zoom -= tool->Position->Z - NavigationPoint->Z;

				if(sender->indexPressed())
				{
					if(ui.GLViewer->view != HDViewport::Perspective)
						ui.GLViewer->view = HDViewport::Custom;

					ui.GLViewer->rotationX += dy/2;
					ui.GLViewer->rotationY += dx/2;
				}
				else
				{
					ui.GLViewer->pntOffset += QPointF(dx * 8 *(ui.GLViewer->zoom/-10.0f) / ui.GLViewer->height(), dy * 8 *(ui.GLViewer->zoom/10.0f) / ui.GLViewer->height());
				}
				*NavigationPoint = *(tool->Position);
			}
		}
		ui.GLViewer->updateGL();
	}
}

void TestWindow::controllerButtonReleased(HDController* sender)
{
	up++;
	ui.txtUp->setText(QString::number(up));
	if(_mainMesh && ui.GLViewer->Tools.count() > 0)
	{
		if(!sender->thumbPressed())
		{
			if(ui.GLViewer->Tools.at(sender->GetControllerId())->PrimaryMode == Selecting)
			{
				ui.GLViewer->first = ui.GLViewer->second = 0;
			}
		}
		ui.GLViewer->updateGL();
	}
}



//Editing properties
void TestWindow::EditFirstToolProperties(HDController *sender)
{
	if(Tool1->PrimaryMode != EditingProperties)
	{
		ui.sbRadius1->setPalette(InactivePalette);
		ui.sbNLength1->setPalette(InactivePalette);
		ui.sbGRate1->setPalette(InactivePalette);
		ui.cmbPMode1->setPalette(InactivePalette);
		ui.cmbSMode1->setPalette(InactivePalette);
		return;
	}
	switch(firstToolProp)
	{
	case 0:
		{
			//radius
			ui.sbRadius1->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.sbRadius1->setValue(Tool1->Radius + 1);
			else if(sender->indexPressed())
				ui.sbRadius1->setValue(Tool1->Radius - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbRadius1->setPalette(InactivePalette);
				ui.sbNLength1->setPalette(ActivePalette);
			}
			break;
		}
	case 1:
		{
			//N Length
			ui.sbNLength1->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.sbNLength1->setValue(Tool1->NeighborsLength + 1);
			else if(sender->indexPressed())
				ui.sbNLength1->setValue(Tool1->NeighborsLength - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbNLength1->setPalette(InactivePalette);
				ui.sbGRate1->setPalette(ActivePalette);
			}
			break;
		}
	case 2:
		{
			//G Rate
			ui.sbGRate1->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.sbGRate1->setValue(Tool1->GrowthRate + 1);
			else if(sender->indexPressed())
				ui.sbGRate1->setValue(Tool1->GrowthRate - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbGRate1->setPalette(InactivePalette);
				ui.cmbPMode1->setPalette(ActivePalette);
			}
			break;
		}
	case 3:
		{
			//primary
			ui.cmbPMode1->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.cmbPMode1->setCurrentIndex((ui.cmbPMode1->currentIndex() + 1) % 7);
			else if(sender->indexPressed())
				ui.cmbPMode1->setCurrentIndex((ui.cmbPMode1->currentIndex() - 1) % 7);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.cmbPMode1->setPalette(InactivePalette);
				ui.cmbSMode1->setPalette(ActivePalette);
			}
			Tool1->PrimaryMode = EditingProperties;
			break;
		}
	case 4:
		{
			//secondary
			ui.cmbSMode1->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.cmbSMode1->setCurrentIndex((ui.cmbSMode1->currentIndex() + 1) % 7);
			else if(sender->indexPressed())
				ui.cmbSMode1->setCurrentIndex((ui.cmbSMode1->currentIndex() - 1) % 7);
			else if(sender->middlePressed())
			{
				firstToolProp = 0;
				ui.cmbSMode1->setPalette(InactivePalette);
				ui.sbRadius1->setPalette(ActivePalette);
			}
			break;
		}
	}
}

void TestWindow::EditSecondToolProperties(HDController *sender)
{
	switch(firstToolProp)
	{
	case 0:
		{
			ui.sbRadius2->setPalette(ActivePalette);
			//radius
			if(sender->thumbPressed())
				ui.sbRadius2->setValue(Tool2->Radius + 1);
			else if(sender->indexPressed())
				ui.sbRadius2->setValue(Tool2->Radius - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbRadius2->setPalette(InactivePalette);
				ui.sbNLength2->setPalette(ActivePalette);
			}
			break;
		}
	case 1:
		{
			//N Length
			ui.sbNLength2->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.sbNLength2->setValue(Tool2->NeighborsLength + 1);
			else if(sender->indexPressed())
				ui.sbNLength2->setValue(Tool2->NeighborsLength - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbNLength2->setPalette(InactivePalette);
				ui.sbGRate2->setPalette(ActivePalette);
			}
			break;
		}
	case 2:
		{
			//G Rate
			ui.sbGRate2->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.sbGRate2->setValue(Tool2->GrowthRate + 1);
			else if(sender->indexPressed())
				ui.sbGRate2->setValue(Tool2->GrowthRate - 1);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.sbGRate2->setPalette(InactivePalette);
				ui.cmbPMode2->setPalette(ActivePalette);
			}
			break;
		}
	case 3:
		{
			//primary
			ui.cmbPMode2->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.cmbPMode2->setCurrentIndex((ui.cmbPMode2->currentIndex() + 1) % 7);
			else if(sender->indexPressed())
				ui.cmbPMode2->setCurrentIndex((ui.cmbPMode2->currentIndex() - 1) % 7);
			else if(sender->middlePressed())
			{
				firstToolProp++;
				ui.cmbPMode2->setPalette(InactivePalette);
				ui.cmbSMode2->setPalette(ActivePalette);
			}
			Tool2->PrimaryMode = EditingProperties;
			break;
		}
	case 4:
		{
			//secondary
			ui.cmbSMode2->setPalette(ActivePalette);
			if(sender->thumbPressed())
				ui.cmbSMode2->setCurrentIndex((ui.cmbSMode2->currentIndex() + 1) % 7);
			else if(sender->indexPressed())
				ui.cmbSMode2->setCurrentIndex((ui.cmbSMode2->currentIndex() + 1) % 7);
			else if(sender->middlePressed())
			{
				firstToolProp = 0;
				ui.cmbSMode2->setPalette(InactivePalette);
				ui.sbRadius2->setPalette(ActivePalette);
			}
			break;
		}
	}
}
//Other stuff
void VMatMult(GLfloat* mat, GLfloat* vec)
{
	GLfloat result[4];

	result[0] = mat[ 0] * vec[0] + mat[ 1] * vec[1] + mat[ 2] * vec[2] + mat[ 3] * vec[3];
	result[1] = mat[ 4] * vec[0] + mat[ 5] * vec[1] + mat[ 6] * vec[2] + mat[ 7] * vec[3];
	result[2] = mat[ 8] * vec[0] + mat[ 9] * vec[1] + mat[10] * vec[2] + mat[11] * vec[3];
	result[3] = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15] * vec[3];

	vec[0] = result[0];
	vec[1] = result[1];
	vec[2] = result[2];
	vec[3] = 1;
}

void TestWindow::UpdateToolLocation(int index, float x, float y, float z)
{
	if(ui.GLViewer->Tools.count() > index)
	{
		GLfloat pos[4];

		pos[0] = x * ui.GLViewer->zoom / (float)ui.GLViewer->width();
		pos[1] = y * ui.GLViewer->zoom / (float)ui.GLViewer->width();
		pos[2] = z * ui.GLViewer->zoom / (float)ui.GLViewer->width();
		pos[3] = 1;

		GLfloat matPos[16];
		matPos[ 0] = 1; matPos[ 1] = 0; matPos[ 2] = 0; matPos[ 3] = -ui.GLViewer->pntOffset.x();
		matPos[ 4] = 0; matPos[ 5] = 1; matPos[ 6] = 0; matPos[ 7] = -ui.GLViewer->pntOffset.y();
		matPos[ 8] = 0; matPos[ 9] = 0; matPos[10] = 1; matPos[11] = 0;
		matPos[12] = 1; matPos[13] = 1; matPos[14] = 1; matPos[15] = 1;

		VMatMult(matPos, pos);

		VMatMult(ui.GLViewer->Minv, pos);

		ui.GLViewer->Tools.at(index)->SetPosition(pos[0], pos[1], pos[2]);

		ui.GLViewer->update();
	}
}

void TestWindow::keyPressEvent(QKeyEvent *event)
{
	if ( event->isAutoRepeat() ) 
 	{
		event->ignore();
		return;
	}

	if(ui.GLViewer->Tools.count() > 0)
	{
		switch(event->key())
		{
		case Qt::Key_F:
			controllers[0]->setThumbPressed(true);
			break;

		case Qt::Key_D:
			controllers[0]->setIndexPressed(true);
			break;

		case Qt::Key_S:
			controllers[0]->setMiddlePressed(true);
			break;

		case Qt::Key_A:
			controllers[0]->setRingPressed(true);
			break;
		default:
			event->ignore();
			break;



		}
		event->accept();
	}
}

void TestWindow::keyReleaseEvent(QKeyEvent * event)
{
	if ( event->isAutoRepeat() ) 
	{
		event->ignore();
		return;
	}
	if(ui.GLViewer->Tools.count() > 0)
	{
   		switch(event->key())
		{
		case Qt::Key_F:
			controllers[0]->setThumbPressed(false);
			break;

		case Qt::Key_D:
			controllers[0]->setIndexPressed(false);
			break;

		case Qt::Key_S:
			controllers[0]->setMiddlePressed(false);
			break;

		case Qt::Key_A:
			controllers[0]->setRingPressed(false);
			break;
		default:
			event->ignore();
			break;



		}
		event->accept();
	}
}

//Shared Memory
void TestWindow::readFromSharedMemory()
{
	if(sharedMemory->isAttached())
	{
		sharedMemory->lock();

		QDataStream in(buffer);
		char *to = (char*)buffer->data().data();
		memcpy(to, sharedMemory->data(), sharedMemory->size());

		sharedMemory->unlock();

		//buffer.setData((char*)sharedMemory->constData(), sharedMemory->size());
		buffer->open(QBuffer::ReadOnly);
		int controllersCount = 0;
		
		in >> controllersCount;

		for(int i = controllers.count() ; i<controllersCount ; i++)
		{
			controllers.append(new HDController(this, i));
			
			connect(controllers[i], SIGNAL(controllerPressed(HDController*)), this, SLOT(controllerButtonPressed(HDController*)));
			connect(controllers[i], SIGNAL(controllerMoving(HDController*)), this, SLOT(controllerButtonMoving(HDController*)));
			connect(controllers[i], SIGNAL(controllerReleased(HDController*)), this, SLOT(controllerButtonReleased(HDController*)));
		}

		for (int i = 0; i < controllers.count(); i++)
		{
			in >> *(controllers[i]);
			UpdateToolLocation(i, controllers[i]->position()->x, controllers[i]->position()->y, controllers[i]->position()->z);
		}
// 		QStringList lstObjectsProperties = strData.split('\n', QString::SkipEmptyParts);
// 
// 		foreach(QString object, lstObjectsProperties)
// 		{
// 			QStringList lstAttributes = object.split(';', QString::SkipEmptyParts);
// 
// 			int ID;
// 			float x, y, z;
// 
// 			foreach(QString attribute, lstAttributes)
// 			{
// 				QStringList lst = attribute.split('=');
// 				lst[0] = lst[0].trimmed();
// 				lst[1] = lst[1].trimmed();
// 
// 				if(lst[0] == "ID")
// 				{
// 					ID = lst[1].toInt();
// 				}
// 
// 				if(lst[0].trimmed() == "Position")
// 				{
// 					lst[1] = lst[1].trimmed().remove(0, 1);
// 					lst[1] = lst[1].remove(lst[1].length() - 1, 1);
// 
// 					QStringList lstCoordinates = lst[1].split(',');
// 
// 					x = lstCoordinates[0].toFloat();
// 					y = lstCoordinates[1].toFloat();
// 					z = lstCoordinates[2].toFloat();
// 				}
// 			}
// 
// 			UpdateToolLocation(ID, x, y, z);
// 		}
	}
}